package com.exrobot.module.rbt.service.robotinfo;

import com.exrobot.framework.common.enums.CommonStatusEnum;
import com.exrobot.framework.common.util.collection.CollectionUtils;
import com.exrobot.framework.common.util.date.LocalDateTimeUtils;
import com.exrobot.module.rbt.controller.admin.order.vo.OrderSaveReqVO;
import com.exrobot.module.rbt.controller.admin.orderitem.vo.OrderItemPageReqVO;
import com.exrobot.module.rbt.dal.dataobject.memberaddress.MemberAddressDO;
import com.exrobot.module.rbt.dal.dataobject.orderitem.OrderItemDO;
import com.exrobot.module.rbt.dal.redis.RbtRedisDAO;
import com.exrobot.module.rbt.enums.RbtOrderStatusEnum;
import com.exrobot.module.rbt.framework.util.CommonUtils;
import com.exrobot.module.rbt.service.memberaddress.MemberAddressService;
import jodd.util.StringUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.exrobot.module.rbt.controller.admin.robotinfo.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotinfo.RobotInfoDO;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.util.object.BeanUtils;

import com.exrobot.module.rbt.dal.mysql.robotinfo.RobotInfoMapper;

import static com.exrobot.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;
import static com.exrobot.module.rbt.enums.ErrorCodeConstants.*;

/**
 * 餐车和盒配置 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class RobotInfoServiceImpl implements RobotInfoService {

    @Resource
    private RobotInfoMapper robotInfoMapper;

    @Resource
    private RbtRedisDAO rbtRedisDAO;

    @Lazy
    @Resource
    private MemberAddressService addressService;

    @Override
    public Long createRobotInfo(RobotInfoSaveReqVO createReqVO) {
        // 插入
        RobotInfoDO robotInfo = BeanUtils.toBean(createReqVO, RobotInfoDO.class);
        robotInfoMapper.insert(robotInfo);

        // 返回
        return robotInfo.getId();
    }

    @Override
    public void updateRobotInfo(RobotInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateRobotInfoExists(updateReqVO.getId());
        // 更新
        RobotInfoDO updateObj = BeanUtils.toBean(updateReqVO, RobotInfoDO.class);
        robotInfoMapper.updateById(updateObj);
    }

    @Override
    public void updateUserStatus(Long id, Integer status) {
        // 校验存在
        validateRobotInfoExists(id);
        RobotInfoDO robotInfoDO = new RobotInfoDO();
        robotInfoDO.setId(id);
        robotInfoDO.setStatus(status);
        robotInfoMapper.updateById(robotInfoDO);
    }

    @Override
    public void deleteRobotInfo(Long id) {
        // 校验存在
        validateRobotInfoExists(id);
        // 删除
        robotInfoMapper.deleteById(id);
    }

    @Override
        public void deleteRobotInfoListByIds(List<Long> ids) {
        // 删除
        robotInfoMapper.deleteByIds(ids);
        }


    private void validateRobotInfoExists(Long id) {
        if (robotInfoMapper.selectById(id) == null) {
            throw exception(RBT_DATA_NOT_EXISTS);
        }
    }

    @Override
    public RobotInfoDO getRobotInfo(Long id) {
        return robotInfoMapper.selectById(id);
    }

    @Override
    public PageResult<RobotInfoDO> getRobotInfoPage(RobotInfoPageReqVO pageReqVO) {
        return robotInfoMapper.selectPage(pageReqVO);
    }


    @Override
    public String getRobotFloor(String sn) {
        String point = rbtRedisDAO.getPoint(sn);
        if(StringUtil.isBlank(point)) {
            return "";
        }
        return CommonUtils.getFloorByPoint(point);
    }

    @Override
    public String[] getRobotLocation(String sn) {
        String point = rbtRedisDAO.getPoint(sn);
        if(StringUtil.isBlank(point)) {
            return null;
        }
        String[] location = point.split(":");
        if(location.length != 3) {
            return null;
        }
        return location;
    }

    @Override
    public Map<String,String> getAllRobotFloorMap() {
        RobotInfoPageReqVO pageReqVO = new RobotInfoPageReqVO();
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        pageReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
        List<RobotInfoDO> list = getRobotInfoPage(pageReqVO).getList();
        if(list == null || list.isEmpty()) {
            return new HashMap<>();
        }
        Map<String,String> map = new HashMap<>();
        for (RobotInfoDO info : list) {
            String point = rbtRedisDAO.getPoint(info.getSn());
            if (StringUtil.isNotBlank(point)) {
                map.put(info.getSn(),"");
                continue;
            }
            String floor = CommonUtils.getFloorByPoint(point);
            if (StringUtil.isBlank(floor)) {
                map.put(info.getSn(),"");
                continue;
            }
            map.put(info.getSn(),floor);
        }
        return map;
    }
    /**
     * 机器人调度计算
     * */
    @Override
    public List<String> RobotScheduling(OrderSaveReqVO orderVO) {
        MemberAddressDO addressDO = addressService.getMemberAddressByPoint(orderVO.getPoint());
        return null;
    }
}